# coding:utf-8
"""
description:UI页面公共类
"""

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from common.Log import LOG


class Base_page:
    def __init__(self, driver):
        self.driver = driver

    def find_element(self, *loc):
        """重写find_element方法，显式等待"""
        try:
            """加*，入参变为元组元素"""
            WebDriverWait(self.driver, 60).until(expected_conditions.visibility_of_element_located(loc))
            return self.driver.find_element(*loc)
        except Exception as e:
            LOG.error("查找元素失败:%s"% e)
            raise e

    def find_elements(self, *loc):
        """重写find_elements方法，显式等待"""
        try:
            WebDriverWait(self.driver, 60).until(expected_conditions.visibility_of_element_located(loc))
            return self.driver.find_elements(*loc)
        except Exception as e:
            LOG.error("查找元素合集失败:%s"% e)
            raise e

    @staticmethod
    def send_keys(value, element):
        """
        重写send_keys方法
        :param value: 输入框输入内容
        :param element: find后的element
        :return:
        """
        try:
            element.clear()
            element.send_keys(value)
        except AttributeError as e:
            LOG.error("输入框填写失败:%s"% e)
            raise e

    def find_toast(self, message):
        """
        根据message在toast提示内容|页面内容进行查找。
        :param message: 需要查找的指定内容
        :return: True and False
        """
        try:
            xp_message = '//*[contains(@text,\"{}\")]'.format(message)
            # 返回实际toast内容
            elements = WebDriverWait(self.driver, 60).until(
                expected_conditions.presence_of_element_located((By.XPATH, xp_message)))
            LOG.info("指定内容已找到，实际完整内容---{}".format(elements.text))
            return True
        except:
            LOG.info("指定内容在页面查找不到，message参数内容---{}".format(message))
            return False

    def assertVisible(self, *loc):
        """
        检查指定元素在页面中是否存在
        :param loc: loc变量值
        :return: 存在返回Ture，不存在返回False
        """
        try:
            WebDriverWait(self.driver, 60).until(expected_conditions.visibility_of_element_located(loc))
            self.driver.find_element(*loc)
            return True
        except:
            # LOG.info("指定元素在页面查找不到，元素信息---{}".format(*loc))
            return False
